package com.youlai.boot.bmp.service.impl;

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.bmp.mapper.ProcessCategoryMapper;
import com.youlai.boot.bmp.service.ProcessCategoryService;
import com.youlai.boot.bmp.model.entity.ProcessCategory;
import com.youlai.boot.bmp.model.form.ProcessCategoryForm;
import com.youlai.boot.bmp.model.query.ProcessCategoryQuery;
import com.youlai.boot.bmp.model.vo.ProcessCategoryVO;
import com.youlai.boot.bmp.converter.ProcessCategoryConverter;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.lang.Assert;

/**
 * 流程分类服务实现类
 *
 * @author rabit
 * @since 2025-10-22 21:32
 */
@Service
@RequiredArgsConstructor
public class ProcessCategoryServiceImpl extends ServiceImpl<ProcessCategoryMapper, ProcessCategory> implements ProcessCategoryService {

    private final ProcessCategoryConverter processCategoryConverter;

    /**
    * 获取流程分类分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<ProcessCategoryVO>} 流程分类分页列表
    */
    @Override
    public IPage<ProcessCategoryVO> getProcessCategoryPage(ProcessCategoryQuery queryParams) {
        return this.baseMapper.getProcessCategoryPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
    }

    /**
     * 获取流程分类表单数据
     *
     * @param id 流程分类ID
     * @return 流程分类表单数据
     */
    @Override
    public ProcessCategoryForm getProcessCategoryFormData(Long id) {
        ProcessCategory entity = this.getById(id);
        return processCategoryConverter.toForm(entity);
    }

    /**
     * 新增流程分类
     *
     * @param formData 流程分类表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveProcessCategory(ProcessCategoryForm formData) {
        ProcessCategory entity = processCategoryConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新流程分类
     *
     * @param id   流程分类ID
     * @param formData 流程分类表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateProcessCategory(Long id,ProcessCategoryForm formData) {
        ProcessCategory entity = processCategoryConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除流程分类
     *
     * @param ids 流程分类ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteProcessCategorys(String ids) {
        Assert.isTrue(StringUtils.isNotBlank(ids), "删除的流程分类数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return removeByIds(idList);
    }

}
